﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BOMS.DAL;
using System.Data;

namespace BOMS.WebUI
{
    public partial class sysParameterSetting : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                bindData();
            }
        }
        private void bindData()
        {
            DataTable dt = SysAdmin.getParameter();
            this.lanName.Text=dt.Rows[0]["lanName"].ToString();
            this.refresh.Text=dt.Rows[0]["refresh"].ToString();
            //this.groupVMin.Text=dt.Rows[0]["groupVMin"].ToString();
            //this.groupVMax.Text=dt.Rows[0]["groupVMax"].ToString();
            //this.groupIMin.Text=dt.Rows[0]["groupIMin"].ToString();
            //this.groupIMax.Text=dt.Rows[0]["groupIMax"].ToString();
            this.singleVMin.Text=dt.Rows[0]["singleVMin"].ToString();
            this.singleVMax.Text=dt.Rows[0]["singleVMax"].ToString();
            this.singleIMin.Text=dt.Rows[0]["singleIMin"].ToString();
            this.singleIMax.Text = dt.Rows[0]["singleIMax"].ToString();
            this.temp.Text=dt.Rows[0]["tempMax"].ToString();
           // this.msgAlarm.Text = dt.Rows[0]["msgAlarm"].ToString();
           // this.mailAlarm.Text = dt.Rows[0]["mailAlarm"].ToString();
            this.lbMusic.Text= dt.Rows[0]["musicAlarm"].ToString();
            //this.lbScreen.Text = dt.Rows[0]["screenAlarm"].ToString();

        }
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            string lanName=this.lanName.Text;
            int refresh = Convert.ToInt32(this.refresh.Text);
            decimal groupVMin = 0;// Convert.ToDecimal(this.groupVMin.Text);
            decimal groupVMax = 0;// Convert.ToDecimal(this.groupVMax.Text);
            decimal groupIMin = 0;// Convert.ToDecimal(this.groupIMin.Text);
            decimal groupIMax = 0;// Convert.ToDecimal(this.groupIMax.Text);
            decimal singleVMin = Convert.ToDecimal(this.singleVMin.Text);
            decimal singleVMax = Convert.ToDecimal(this.singleVMax.Text);
            decimal singleIMin = Convert.ToDecimal(this.singleIMin.Text);
            decimal singleIMax = Convert.ToDecimal(this.singleIMax.Text);
            decimal tempMax = Convert.ToDecimal(this.temp.Text);
            string msgAlarm = "";// this.msgAlarm.Text;
            string mailAlarm = "";// this.mailAlarm.Text;
            string musicAlarm = uploadMusic();
            string screenAlarm = "";//uploadPic();
            
           
            Boolean isSuccess = SysAdmin.setParameter( lanName,  refresh,  groupVMin,  groupVMax,  groupIMin,  groupIMax,  singleVMin,  singleVMax,  singleIMin,  singleIMax,  tempMax,  msgAlarm,  mailAlarm,  musicAlarm,  screenAlarm);
            Type cstype = this.GetType();
            if (isSuccess)
            {
                ClientScript.RegisterStartupScript(cstype, "key", "<script>alert('修改成功！');window.location.href=window.location.href</script>");
                bindData();
            }
            else
            {
                ClientScript.RegisterStartupScript(cstype, "key", "<script>alert('修改失败！');</script>");
            }
        }
        private string uploadMusic()
        {
            string rt = "";
            string fileName = musicAlarm.PostedFile.FileName; //获取初始文件名
            if (fileName != "")
            {
                   
                int i = fileName.LastIndexOf("."); //取得文件名中最后一个"."的索引   
                string newext = fileName.Substring(i); //获取文件扩展名   
                if (newext.ToLower() != ".wav")
                {
                    Response.Write("只允许上传*.wav格式的声音文件!");
                    Response.End();
                }
                DateTime now = DateTime.Now; //获取系统时间   
                string fileNameNew = "alarm.wav";// now.Millisecond.ToString() + "_" + fileName.Replace("'", " ").Trim(); //重新为文件命名,时间毫秒部分+文件大小+扩展名   
               
                try
                {
                    musicAlarm.PostedFile.SaveAs(Server.MapPath("Alarm\\music\\" + fileNameNew)); // 保存文件到路径,用Server.MapPath()取当前文件的绝对目录.在asp.net里"\"必须用""代替   
                    rt = fileNameNew;
                }
                catch (Exception ex)
                {
                    Response.Write("声音文件上传失败!");
                
                }
            }
            return rt;
        }
        //private string uploadPic()
        //{
        //    string rt = "";
        //    string fileName = screenAlarm.PostedFile.FileName; //获取初始文件名
        //    if (fileName != "")
        //    {
        //        int i = fileName.LastIndexOf("."); //取得文件名中最后一个"."的索引   
        //        string newext = fileName.Substring(i); //获取文件扩展名   
        //        if (newext.ToLower() != ".jpg" && newext.ToLower() != ".gif" && newext.ToLower() != ".jpeg")
        //        {
        //            Response.Write("只允许上传jpg,gif,jpeg格式的图片文件!");
        //            Response.End();
        //        }
        //        DateTime now = DateTime.Now; //获取系统时间   
        //        string fileNameNew = now.Millisecond.ToString() + "_" + fileName.Replace("'", " ").Trim(); //重新为文件命名,时间毫秒部分+文件大小+扩展名   

        //        try
        //        {
        //            screenAlarm.PostedFile.SaveAs(Server.MapPath("Alarm\\pic\\" + fileNameNew)); // 保存文件到路径,用Server.MapPath()取当前文件的绝对目录.
        //            rt = fileNameNew;
        //        }
        //        catch (Exception ex)
        //        {
        //            Response.Write("图片文件上传失败!");

        //        }
        //    }
        //    return rt;
        //}
    }
}